<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends Auth_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model("user_model","user");
    }

    public function userDetail(){
        $userInfo = $this->userDetail;
        $pointStatus = $this->user->hasSetPoint($userInfo['AccountID']);
        $userInfo['hasSetPoint'] = $pointStatus;
        $this->createApi($userInfo);
        exit();
    }
    //充值
    public function recharge(){
        $recharge_ordernum = $this->json_content['recharge_ordernum'];
        $recharge_money = $this->json_content['recharge_money'];
        if(empty($recharge_ordernum)||empty($recharge_money)){
            $this->errorCode = 60004;
            $this->createApi();exit();
        }
        $userInfo = $this->userInfo($this->token);
        $data = array(
            'recharge_ordernum'=>$recharge_ordernum,
            'recharge_money'=>$recharge_money,
            'AccountID'=>$userInfo['AccountID'],
            'createtime'=>time()
            );

        $result = $this->user->toRecharge($data);
        if(!$result){
        $this->errorCode = API_ERROR_HOTEL_LIMIT;
    }
    $this->createApi();
    }
    
    public function sendPoint(){
        $params['points'] = $this->json_content['points'];
        $params['mobile'] =$this->json_content['mobile'];
        // $params['points'] = 2;
  //       $params['mobile'] ="15624997044";
        $validate = new Validate([
            'points' => 'require|number',
            'mobile' => 'require|string',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        $userInfo = $this->userDetail;
        if($params['points'] > $myPoints['points']){
            $this->errorCode = 90004;
            $this->createApi();exit();
        }
        $toUserInfo= $this->user->get_userinfo("", $params['mobile']);
        $result = 0;
        if(isset($toUserInfo['AccountID']))
        {
             $this->load->model("point_model","point");

             $result = $this->point->toSendPoints($userInfo['AccountID'], $toAccountID['AccountID'], $params['points']);
             
        }
       if(empty($result))
       {
           $this->errorCode = 50006;
       }
        $this->createApi();exit;
    }

    //签到
    public function addpoints(){
        $points  = $this->setPoint(2);
        $point_jieti  = $this->setPoint(6);
        $point_max = $this->setPoint(7);
        $lianxu = $this->setPoint(8);
        $userInfo = $this->userDetail;
        $continuing_times = 1;
        $times = $this->point->getPointByTime(date("Y-m-d",time()-24*60*60),$userInfo['AccountID']);
        if($times['continuing_times'])
        {
            if($times['continuing_times'] <= $lianxu)
            {
                $points += ($times['continuing_times']+1);
                if($pints>$point_max)
                {
                    $pints=$point_max;
                }
                $continuing_times +=$point_jieti*$times['continuing_times'];
            }else{
                $points  = $point_max;
            }
            
        }
        $data = array(
            'AccountID'=>$userInfo['AccountID'],
            'createtime'=>date('Y-m-d',time()),
            'points'=>$points,
            'type'=>2,
            'continuing_times'=>$continuing_times
            );
        $this->load->model("Point_model","point");
        $res = $this->point->signed($userInfo['AccountID'], $data);
        if($res === "hassignin"){
            $this->errorCode = 70002;
            $this->createApi();exit;    
        }
        $this->createApi(['addPointCnt'=>$points]);
    }

    //获取积分记录
    public function pointRecord()
    {
        $userInfo = $this->userDetail;
        $fields = " points, createtime, type, AccountID";
        $list = $this->user->getPointRecord($userInfo['AccountID'], $fields);
        foreach ($list as $key => $value) {
            $list[$key]['points'] = abs($value['points']);
        }
        $this->createApi($list);
    }



    public function hasSetPoint(){
        $userInfo = $this->userInfo($this->token);
        $AccountID = $userInfo['AccountID'];
        $res = $this->user->hasSetPoint($AccountID);
        $this->createApi(array("isSetPoint"=>$res));
    }


    /*修改个人信息*/
    public function editUserInfo(){
        $AccountID = $this->userDetail['AccountID'];
        $data = array();
        $avatar = trim($this->queryData["avatar"]);
        if($avatar){
            $data['avatar'] = $avatar;
        }
        $nickname = trim($this->queryData["nickname"]);
        if($nickname){
            $data['nickname'] = $nickname;
        }
        $sex = trim($this->queryData["sex"]);

        if($sex){
            $data['sex'] = $sex;
        }
        $region = trim($this->queryData["region"]);
        if($region){
            $data['region'] = $region;
        }
        $birthday = trim($this->queryData["birthday"]);
        if($birthday){
            $data['birthday'] = $birthday;
        }
        if(empty($data)){
            $this->errorCode = 50003;
            $this->createApi();exit();
           
        }
        $res = $this->user->edit_user($AccountID, $data);
        $this->createApi($res);

    }

    public function searchUser(){
        $keyword = $this->json_content['keyword'];
        if(empty($keyword)){
            $this->errorCode = API_ERROR_PARAMS_MISS;
            $this->createApi();exit();
        }
        $filds = " AccountID,Username,NickName,avatar,sex,region,trueName ";
        $userInfo = $this->user->get_userinfo_by_keyword($keyword, $filds);
        if($userInfo['region']){
            $fields = " id, name ,pid";
            if($userInfo['region']){
            $city = $this->address->getRegionById($userInfo['region'], $fields);
                if($city['pid']){
                    $province = $this->address->getRegionById($city['pid'], $fields);
                }
            }
            $userInfo['region'] = array('province'=>$province,'city'=>$city);
        }
      
        if($userInfo['avatar']){
            $userInfo['avatar'] = imgFormat($userInfo['avatar']);
        }
        $this->createApi($userInfo);
        exit();
    }


    public function orderList(){
        $userInfo = $this->userInfo($this->token);
        $AccountID = $userInfo['AccountID'];
        $orderList = $this->user->getOrderList($AccountID);
        foreach($orderList as $key=>$value){
            $orderList[$key]['createtime'] = $value['createtime'];
            unset($orderList[$key]['isDel']);
        }
        $this->createApi($orderList); 
   }


    //我的收藏

    public function myCollection(){

        $per_page = $this->queryData['per_page'];
        $keyword = $this->queryData['keyword'];
        $limit = $this->config->item('limit');
        $this->load->model("News_model","news");
        $filter = ['keyword'=>$keyword];
        $collectionList = $this->user->getMyCollection($filter,$this->userDetail['username'], $per_page, $limit);
        if ($collectionList) {
            foreach ($collectionList as $key => $value) {
                //新闻
                $newsDetail = [];
                $newsDetail['addtime'] = $value['addtime']?date("Y-m",$value['addtime']):0; 
                $newsDetail['cover'] = $value['cover']?OSSHOST.$value['cover']:0;
                $newsDetail['content'] = str_replace('/upload/',OSSHOST.'upload/',$value['content']);
                $newsDetail['reivewCnt'] = intval($this->news->getNewsReviewCnt($value['newsID']));
                $newsDetail['transferfrom'] = $value['transferfrom'];
                $newsDetail['title'] = $value['title'];

                $collectionList[$key]['newsInfo'] = $newsDetail?$newsDetail:[];
                unset($collectionList[$key]['addtime'],$collectionList[$key]['cover'],$collectionList[$key]['transferfrom'],$collectionList[$key]['content'],$collectionList[$key]['title']);
            }        
        }
        $this->createApi($collectionList); exit();
    }

    public function resetPwd(){
        $token = $this->json_content['token'];
        $password = $this->json_content['password'];
    }


    public function myActivityList(){
        $this->isArrays = true;
        $params['per_page'] = $this->queryData['per_page'];
        $validate = new Validate([
            'per_page' => 'require|number',
        ]);
        if (!$validate->check($params)) {
            $this->errorCode = 50003;
            $this->errorMessage = $validate->getError();
            $this->createApi();
            exit;
        }
        
        $limit  = $this->config->item('limit');
        $userInfo = $this->userDetail;
        $params['AccountID'] = $userInfo['AccountID'];
        $this->load->model('News_model', 'news');
        $newsList = $this->news->getMyActivityList($params,$params['per_page'], $limit," DISTINCT n.newsID,n.title,n.transferfrom,n.content,n.addtime,n.cover,n.categoryid,ques.questionID");
        $userInfo = $this->userDetail;
        $this->load->model('Examination_model', 'examination');
        foreach ($newsList as $key => $value) {
            $answerData = ['AccountID'=>$userInfo['AccountID'],'newsID'=>$value['newsID'],'questionID'=>$value['questionID']];
            $newsList[$key]['statue'] = $this->examination->hasAnswered($answerData);
            $newsList[$key]['cover'] = $value['cover']?OSSHOST.$value['cover']:'';
            $newsList[$key]['content'] = cutstr(strip_tags($value['content']),30);
            $newsList[$key]['addtime'] = humanDate($value['addtime']);
            $newsList[$key]['reivewCnt'] = intval($this->news->getNewsReviewCnt($value['newsID']));
        }
        $this->createApi($newsList); exit();
    }

//设置角色启用状态
    public function editStatus(){
        $id = $this->input->post("id",true);
        $status = $this->input->post("status",true) ? $this->input->post("status",true) :-1;
        $res = $this->user->editStatus($id,$status);
        echo $res;
    }

}
